package com.ucode.springboot.starter.fileStorage;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.ucode.springboot.starter.fileStorage.utils.FilePathHelper;
import com.ucode.springboot.starter.fileStorage.utils.FileExtensionConvertConvert;
import com.ucode.tool.util.IdentifyUtils;

import cn.hutool.core.util.StrUtil;
import net.sf.jmimemagic.Magic;
import net.sf.jmimemagic.MagicMatch;

/**
 * 上传文件信息
 * @author: liliang
 * @date: 2020年6月10日 上午10:42:31
 */
public class UploadObject {

    private String fileName;//文件名称
    private String mimeType;//资源媒体类型
    private String catalog;//文件目录
    private String url;//远程文件地址(把远程文件fetch到存储源)
    private byte[] fileBytes;//文件
    private File file;//文件
    private InputStream fileInputStream;//文件
    private Map<String, Object> metadata = new HashMap<String, Object>();
    
    public UploadObject(String filePath) {
        if (filePath.startsWith(FilePathHelper.HTTP_PREFIX) || filePath.startsWith(FilePathHelper.HTTPS_PREFIX)) {
            this.url = filePath;
            this.fileName = FilePathHelper.parseFileName(this.url);
        } else {
            this.file = new File(filePath);
            this.fileName = file.getName();
        }
    }
    
    public UploadObject(File file) {
        this.fileName = file.getName();
        this.file = file;
    }

    public UploadObject(String fileName, File file) {
        this.fileName = fileName;
        this.file = file;
    }

    public UploadObject(String fileName, InputStream inputStream, String mimeType) {
        this.fileName = fileName;
        this.fileInputStream = inputStream;
        this.mimeType = mimeType;
    }

    public UploadObject(String fileName, byte[] bytes, String mimeType) {
        this.fileName = fileName;
        this.fileBytes = bytes;
        this.mimeType = mimeType;
    }

    public UploadObject(String fileName, byte[] bytes) {
        this.fileName = fileName;
        this.fileBytes = bytes;
        this.mimeType = perseMimeType(bytes);
    }

    public String getFileName() {
        if (StrUtil.isBlank(fileName)) {
            fileName = IdentifyUtils.randomUUID().replaceAll("\\-", "");
        }
        if (mimeType != null && !fileName.contains(".")) {
            String fileExtension = FileExtensionConvertConvert.getFileExtension(mimeType);
            if(fileExtension != null)fileName = fileName + fileExtension;
        }
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    public String getCatalog() {
        return catalog;
    }

    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public byte[] getFileBytes() {
        return fileBytes;
    }

    public void setFileBytes(byte[] fileBytes) {
        this.fileBytes = fileBytes;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public InputStream getFileInputStream() {
        return fileInputStream;
    }

    public void setFileInputStream(InputStream fileInputStream) {
        this.fileInputStream = fileInputStream;
    }

    public Map<String, Object> getMetadata() {
        return metadata;
    }

    public void setMetadata(Map<String, Object> metadata) {
        this.metadata = metadata;
    }
    
    public UploadObject toCatalog(String catalog) {
        this.catalog = catalog;
        return this;
    }
    
    private static String perseMimeType(byte[] bytes){
        try {
            MagicMatch match = Magic.getMagicMatch(bytes);
            String mimeType = match.getMimeType();
            return mimeType;
        } catch (Exception e) {
            return null;
        }
    }
    
    
}
